#设置最小CMAKE版本
cmake_minimum_required(VERSION 3.13)
#设置工程名称及编译器
project(VideoSubFinder LANGUAGES CXX)

set(WHOLE_ARCHIVE_FLAGS "-Wl,--whole-archive")
set(NO_WHOLE_ARCHIVE_FLAGS "-Wl,--no-whole-archive")


#保留CUDA编译选项
if(WIN32)
	option(WIN64 "Make x64 build" ON)

	if(WIN64)
		find_package(IntelDPCPP REQUIRED)
		option(USE_CUDA "Build with CUDA support" ON)
		add_definitions(-DWIN64)
		set(wxWidgets_INCLUDE_DIRS "$ENV{WX_WIDGETS_PATH}/lib/vc_x64_lib/mswu;$ENV{WX_WIDGETS_PATH}/include" CACHE STRING "Paths to OpenCV include dirs")
		set(OpenCV_INCLUDE_DIRS "$ENV{OPENCV_BUILD_PATH_X64}/install/include" CACHE STRING "Paths to OpenCV include dirs")
		set(FFMPEG_INCLUDE_DIRS "$ENV{FFMPEG_PATH_X64}/include" CACHE STRING "Paths to FFMPEG include dirs")
		set(VideoSubFinder_LINK_DIRS "$ENV{WX_WIDGETS_PATH}/lib/vc_x64_lib;$ENV{OPENCV_BUILD_PATH_X64}/lib/Release;$ENV{CUDA_TOOLKIT_PATH}/lib/x64;$ENV{FFMPEG_PATH_X64}/lib" CACHE STRING "VideoSubFinderWXW link dirs")
	else()
		option(USE_CUDA "Build with CUDA support" OFF)
		add_definitions(-DWINX86)
		set(wxWidgets_INCLUDE_DIRS "$ENV{WX_WIDGETS_PATH}/lib/vc_lib/mswu;$ENV{WX_WIDGETS_PATH}/include" CACHE STRING "Paths to OpenCV include dirs")
		set(OpenCV_INCLUDE_DIRS "$ENV{OPENCV_BUILD_PATH_X86}/install/include" CACHE STRING "Paths to OpenCV include dirs")
		set(FFMPEG_INCLUDE_DIRS "$ENV{FFMPEG_PATH_X86}/include" CACHE STRING "Paths to FFMPEG include dirs")
		set(VideoSubFinderWXW_LINK_DIRS "$ENV{WX_WIDGETS_PATH}/lib/vc_lib;$ENV{OPENCV_BUILD_PATH_X86}/lib/Release;$ENV{FFMPEG_PATH_X86}/lib" CACHE STRING "VideoSubFinderWXW link dirs")
	endif(WIN64)

	set(OpenCV_LIBS "opencv_world$ENV{OPENCV_LIBS_VER}.lib" CACHE STRING "Required OpenCV libs for build")

else()
	option(USE_CUDA "Build with CUDA support" ON)
	if(CMAKE_BUILD_TYPE STREQUAL "Debug")
		set(wxWidgets_USE_DEBUG ON)
	else()
		set(wxWidgets_USE_DEBUG OFF)
	endif()
	find_package(wxWidgets REQUIRED COMPONENTS core base aui)
	find_package(OpenCV REQUIRED)
	if(USE_CUDA)
		set(CUDA_LINK_DIRS "$ENV{CUDA_TOOLKIT_PATH}//lib64;$ENV{CUDA_TOOLKIT_PATH}//extras/CUPTI/lib64" CACHE STRING "CUDA link dirs")
	endif(USE_CUDA)
endif(WIN32)

if(USE_CUDA)
	set(CMAKE_CUDA_FLAGS "-arch=all")
	add_definitions(-DUSE_CUDA)
	set(CUDAKernels_INCLUDE_DIRS "CUDAKernels")
	set(CUDAKernels_LIB "CUDAKernels;cudart;nppicc;nppig")
	add_subdirectory(CUDAKernels)
else()
	set(CUDAKernels_INCLUDE_DIRS "")
add_subdirectory(IPAlgorithms)
	set(CUDAKernels_LIB "")
endif(USE_CUDA)

add_subdirectory(OCVVideo)
add_subdirectory(FFMPEGVideo)


file(GLOB VideoSubFinder_src
     "VideoSubFinder/*.h"
     "VideoSubFinder/*.cpp"
)

if (WIN32)
	add_executable(VideoSubFinder WIN32 ${VideoSubFinder_src})
else()
	add_executable(VideoSubFinder ${VideoSubFinder_src})
endif (WIN32)

target_compile_features(VideoSubFinder PUBLIC cxx_std_17)
set_target_properties(VideoSubFinder PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_include_directories(VideoSubFinder PUBLIC
	${wxWidgets_INCLUDE_DIRS}
	 ${OpenCV_INCLUDE_DIRS}
	"./Include"
	"./IPAlgorithms"
	"./OCVVideo"
	"./FFMPEGVideo"
)

if (WIN32)
	target_link_directories(VideoSubFinder  PUBLIC
		${VideoSubFinder_LINK_DIRS}
	)

	target_link_libraries(VideoSubFinder PUBLIC
		${WHOLE_ARCHIVE_FLAGS}
		IPAlgorithms
		OCVVideo
		FFMPEGVideo
		${NO_WHOLE_ARCHIVE_FLAGS}
		${CUDAKernels_LIB}
		wxmsw32u_aui.lib
		wxmsw32u_media.lib
		wxmsw32u_core.lib
		wxmsw32u_adv.lib
		wxbase32u.lib
		wxtiff.lib
		wxjpeg.lib
		wxpng.lib
		wxzlib.lib
		wxregexu.lib
		wxexpat.lib
		winmm.lib
		comctl32.lib
		rpcrt4.lib
		wsock32.lib
		odbc32.lib
		vfw32.lib
		avdevice.lib
		avformat.lib
		avfilter.lib
		avcodec.lib
		swresample.lib
		swscale.lib
		avutil.lib
		${OpenCV_LIBS}
	)
else()
	target_compile_definitions(VideoSubFinder PUBLIC
		${wxWidgets_DEFINITIONS}
		$<$<CONFIG:Debug>:${wxWidgets_DEFINITIONS_DEBUG}>)
	target_compile_options(VideoSubFinder PRIVATE ${wxWidgets_CXX_FLAGS})

	target_link_directories(VideoSubFinder  PUBLIC
		${wxWidgets_LIBRARY_DIRS}
		${CUDA_LINK_DIRS}
	)

	target_link_libraries(VideoSubFinder PUBLIC
		${WHOLE_ARCHIVE_FLAGS}
		IPAlgorithms
		OCVVideo
		FFMPEGVideo
		${NO_WHOLE_ARCHIVE_FLAGS}
		${CUDAKernels_LIB}
		${wxWidgets_LIBRARIES}
		${OpenCV_LIBS}
		avcodec
		avformat
		avutil
		swscale
		avfilter
		tbb
		X11
	)
endif (WIN32)
